//
//  StringUtil.m
//  Platform
//
//  Created by 张宁宁 on 16/9/22.
//  Copyright © 2016年 lirenkj. All rights reserved.
//

#import "StringUtil.h"
#import "GTMBase64.h"


@implementation StringUtil

+ (NSString *)jion:(NSArray *)array separator:(NSString *)sep
{
    return [array componentsJoinedByString:sep];
}

+ (NSArray *)splitString:(NSString *)string separator:(NSString *)sep
{
    return [string componentsSeparatedByString:sep];
}


+ (BOOL)isEmpty:(NSString *)string needTrim:(BOOL)isTrim
{
    if(string == nil || (NSNull *)string == [NSNull null] || [string isEqualToString:@""]){
        return YES;
    }
    NSString *tmp = string;
    if(isTrim){
        tmp = [tmp stringByTrimmingCharactersInSet:[NSCharacterSet
                                                    whitespaceAndNewlineCharacterSet]];
    }
    if([tmp length] == 0){
        return YES;
    }else{
        return NO;
    }
}

+ (NSString *)trim:(NSString *)string
{
    return [string stringByTrimmingCharactersInSet:[NSCharacterSet
                                                    whitespaceAndNewlineCharacterSet]];
}

+ (BOOL)isContain:(NSString *)string searchStr:(NSString *)str
{
    BOOL flag = NO;
    if(string != nil){
        NSRange range = [string rangeOfString:str];
        if(range.location != NSNotFound){
            flag = YES;
        }
    }
    return flag;
}

+ (NSString *)replace:(NSString *)string
            searchStr:(NSString *)str
           replaceStr:(NSString *)replaceStr{
    return [string stringByReplacingOccurrencesOfString:str withString:replaceStr];
}

+ (NSString *)subString:(NSString *)string startIndex:(int)start endIndex:(int)end
{
    int length = end - start;
    NSRange range = NSMakeRange(start, length);
    return [string substringWithRange:range];
}

+ (NSString *)subString:(NSString *)string startIndex:(int)start
{
    NSRange range = NSMakeRange(start, [string length]-start);
    return [string substringWithRange:range];
}

+ (BOOL)startWith:(NSString *)string searchStr:(NSString *)searchStr
{
    NSRange range = [string rangeOfString:searchStr];
    if(range.location == 0){
        return YES;
    }else{
        return NO;
    }
}

+ (BOOL)endWith:(NSString *)string searchStr:(NSString *)searchStr
{
    NSRange range = [string rangeOfString:searchStr options:NSBackwardsSearch];
    long index = string.length - searchStr.length;
    if(index == range.location){
        return YES;
    }else{
        return NO;
    }
}

+ (BOOL)equals:(NSString *)string otherStr:(NSString *)otherStr
{
    return [string isEqualToString:otherStr];
}

+ (BOOL)equalsIgnoreCase:(NSString *)string otherStr:(NSString *)otherStr
{
    return [[string lowercaseString] isEqualToString:[otherStr lowercaseString]];
}

+ (int)indexOf:(NSString *)string searchStr:(NSString *)searchStr
{
    NSRange range = [string rangeOfString:searchStr];
    if(range.location != NSNotFound){
        return [NSString stringWithFormat:@"%lu",(unsigned long)range.location].intValue;
    }else{
        return -1;
    }
}

+ (int)lastIndexOf:(NSString *)string searchStr:(NSString *)searchStr
{
    NSRange range = [string rangeOfString:searchStr options:NSBackwardsSearch];
    if(range.location != NSNotFound){
        return [NSString stringWithFormat:@"%lu",(unsigned long)range.location].intValue;
    }else{
        return -1;
    }
}
+ (NSString *)stringToBase64:(NSString *)string
{
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 encodeData:data];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

+ (NSString *)base64ToString:(NSString *)string
{
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 decodeData:data];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

+ (BOOL)checkoutPhoneNum: (NSString *)phoneNum {
    NSString *regexStr = @"^1[3,8]\\d{9}|14[5,6,7,8,9]\\d{8}|15[^4]\\d{8}|16[6]\\d{8}|17[^9]\\d{8}|19[8,9]\\d{8}$";
    NSError *error;
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    if (error) return NO;
    NSInteger count = [regular numberOfMatchesInString:phoneNum options:NSMatchingReportCompletion range:NSMakeRange(0, phoneNum.length)];
    if (count > 0) {
        return YES;
    } else {
        return NO;
    }
}

+ (BOOL)checkUserID:(NSString *)userID{
    
    //长度不为18的都排除掉
    if (userID.length!=18) {
        return NO;
    }
    
    //校验格式
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    BOOL flag = [identityCardPredicate evaluateWithObject:userID];
    
    if (!flag) {
        return flag;    //格式错误
    }else {
        //格式正确再判断是否合法
        
        //将前17位加权因子保存在数组里
        NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
        
        //这是除以11后，可能产生的11位余数、验证码，也保存成数组
        NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
        
        //用来保存前17位各自乖以加权因子后的总和
        NSInteger idCardWiSum = 0;
        for(int i = 0;i < 17;i++)
        {
            NSInteger subStrIndex = [[userID substringWithRange:NSMakeRange(i, 1)] integerValue];
            NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
            
            idCardWiSum+= subStrIndex * idCardWiIndex;
            
        }
        
        //计算出校验码所在数组的位置
        NSInteger idCardMod=idCardWiSum%11;
        
        //得到最后一位身份证号码
        NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)];
        
        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        if(idCardMod==2)
        {
            if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
            {
                return YES;
            }else
            {
                return NO;
            }
        }else{
            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
            if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
    }
}

#pragma mark- 修改数字展示
+ (NSString *)changeLookNumberToNeed:(NSString *)numberString{
    
    NSString *changeString = numberString;
    if ([numberString floatValue] >= 1000 && [numberString floatValue] < 999944) {
        
        changeString = [NSString stringWithFormat:@"%.1fK",[numberString floatValue]/1000];
    }
    if ([numberString floatValue] >= 999944){
        
        changeString = @"999.9K";
    }
    
    return changeString;
}

#pragma mark- 将时间戳转换为时间
+ (NSString *)changeDateStringToDate:(NSString *)dateString{
    
    dateString = [NSString stringWithFormat:@"%@",dateString];
    if (dateString.length == 13) {
        dateString = [dateString substringToIndex:10];
    }
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[dateString longLongValue]];
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd  HH:mm"];
    return [formater stringFromDate:confromTimesp];
}
@end
